package Socket;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * Created by sunwei on 2017/9/29
 * Time:14:52
 */
public class Socketclient {
    public static void main(String[] args) {
        Socket socket = null;
        BufferedReader bufferedReader =null;
        PrintWriter printWriter = null;
        try {
            socket = new Socket("127.0.0.1",4567);//设置客户端的socket
            System.out.println("Socket="+socket);
            //用于发送给server端的信息
            printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            //用于接收server端发来的消息
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            printWriter.println("howdy ");
            printWriter.flush();
            String str = bufferedReader.readLine();
            System.out.println("拿到server返回的消息"+str);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        finally {
            try {
                System.out.println("close........");
                bufferedReader.close();
                printWriter.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
